//
//  ZJUserInfoTools.swift
//  Webo
//
//  Created by 曾静 on 16/6/26.
//  Copyright © 2016年 qq. All rights reserved.
//

import UIKit

class ZJUserInfoTools: NSObject {
    
    
    var isLogin:Bool{
        
        return token != nil
    }
    
    var userAccount:ZJUserAccount?
    
    var token:String?{
        if userAccount?.access_token == nil{
            return nil
        }else{
            
            if userAccount?.expires_Data?.compare(NSDate()) == NSComparisonResult.OrderedDescending{
                return userAccount?.access_token

            }else{
                return nil
            }
        }
        
    }
    
    override init() {
        super.init()
        
        //接档!!!!!!!!!
        userAccount = getUserInfo()
    }

    static let shareUserInfo:ZJUserInfoTools = ZJUserInfoTools()
    

    let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
    
    func getUserInfo()->ZJUserAccount?{
        
     let userInfo = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as?ZJUserAccount
        
     return userInfo
        
    }
    
    func saveUserInfo(userAccount:ZJUserAccount){
        
        self.userAccount = userAccount
        
        print(isLogin)
        
        NSKeyedArchiver.archiveRootObject(userAccount, toFile: path)
        
        
    }
    
}
